//
//  Sqlite4Cocoa2dxTests.m
//  Sqlite4Cocoa2dxTests
//
//  Created by yanglishuan on 13-9-4.
//
//

#import "Sqlite4Cocoa2dxTests.h"
#import "Sqlite3Utils.h"

@interface Sqlite4Cocoa2dxTests()
{
//    Sqlite3Utils sqlite3Utils;
}

@end

@implementation Sqlite4Cocoa2dxTests

- (void)setUp
{
    [super setUp];
    
    // Set-up code here.
}

- (void)tearDown
{
    // Tear-down code here.
    
    [super tearDown];
}

void dbInit(char * dbName, sqlite3 **ppDb)
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *db_source_path = [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:dbName] ofType:@"db"];
    NSString *doc_path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *db_des_path = [doc_path stringByAppendingFormat:@"/%@.db", [NSString stringWithUTF8String:dbName]];
    if (![fileManager fileExistsAtPath:db_des_path]) {
        BOOL rc = [fileManager copyItemAtPath:db_source_path toPath:db_des_path error:nil];
        ;
        printf("---------------------\n数据库文件从%s复制到%s%s\n---------------------\n", [db_source_path cStringUsingEncoding:NSUTF8StringEncoding], [db_des_path cStringUsingEncoding:NSUTF8StringEncoding], rc ? "成功": "失败");
    }
    if(sqlite3_open_v2([db_des_path cStringUsingEncoding:NSUTF8StringEncoding], ppDb, SQLITE_OPEN_READWRITE, NULL) == SQLITE_OK) {
        std::cout << "数据库初始化成功！" << std::endl;
    } else {
        std::cout << "数据库初始化失败" << std::endl;
        sqlite3_close(*ppDb);
    }
    sqlite3_close(*ppDb);
}

- (void)testSqlite3UtilsInit
{
    NSString *docunent = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    
    const char *dbName = "person";
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *db_source_path = [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:dbName] ofType:@"db"];
    NSString *doc_path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *db_des_path = [doc_path stringByAppendingFormat:@"/%@.db", [NSString stringWithUTF8String:dbName]];
    if (![fileManager fileExistsAtPath:db_des_path]) {
        BOOL rc = [fileManager copyItemAtPath:db_source_path toPath:db_des_path error:nil];
        printf("\n---------------------\n数据库文件从%s复制到%s%s\n---------------------\n", [db_source_path cStringUsingEncoding:NSUTF8StringEncoding], [db_des_path cStringUsingEncoding:NSUTF8StringEncoding], rc ? "成功": "失败");
    }
    
    cocos2d::CCString dbPath([db_des_path cStringUsingEncoding:NSUTF8StringEncoding]);
    
//    sqlite3Utils = Sqlite3Utils::Sqlite3Utils(dbPath);
}

@end
